package com.example.fridaykt.controller

import com.example.fridaykt.common.base.BaseController
import com.example.fridaykt.common.result.R
import com.example.fridaykt.model.SysMenu
import com.example.fridaykt.service.SysMenuService
import com.example.fridaykt.service.SysRoleMenuService
import com.example.fridaykt.vo.MenuRoleVO
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.web.bind.annotation.*

@RestController
@Tag(name = "用户角色菜单管理接口")
class RoleMenuController(
    private val menuService: SysMenuService,
    private val roleMenuService: SysRoleMenuService
) : BaseController() {
    @GetMapping("/menu/role/{roleId}")
    @Operation(summary = "获取所有菜单以及角色对应的可用菜单id")
    fun allMenuWithRole(@PathVariable("roleId") roleId: Long): R {
        val list: List<SysMenu> = menuService.listWithTree()
        val menuIds: List<Long> = menuService.getMenuIdListByUserIdWithRole(roleId)
        val res: MutableMap<String, Any> = HashMap()
        res["records"] = list
        res["roleHas"] = menuIds
        return R.success(res)
    }

    @PostMapping("/menu/role/{roleId}")
    @Operation(summary = "更新所有菜单以及角色对应的可用菜单id")
    fun allMenuWithRole(@PathVariable("roleId") roleId: Long, @RequestBody vo: MenuRoleVO): R {
        val b: Boolean = roleMenuService.updateByRoleIdAndMenuIds(roleId, vo.ids!!)
        return toAjax(if (b) 1 else 0)
    }
}
